/**
 * Author: Todd Kerpelman
 * Date: 5/18/12
 * Time: 9:47 AM
 *
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package net.kerp.hangoutSpace {

import com.greensock.TweenLite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;

public class SpaceTrails extends Sprite {

  private var _trailBitmapData:BitmapData;
  private var _blurFilter:BlurFilter;
  private var _noiseBitmapData:BitmapData;
  private var _noiseBitmapSeed:int;
  private var _offsets:Array;
  private var _wind:SpaceWind;
  private var _prototypeSpaceship:Shape;

  public function SpaceTrails(wind:SpaceWind) {
    _wind = wind;

    createPrototypShape();
    setupBitmapData();
    addEventListener(Event.ENTER_FRAME, onFrame);
  }

  private function createPrototypShape():void {
    _prototypeSpaceship = new Shape();
    _prototypeSpaceship.graphics.beginFill(0x000000);
    // Try drawing a little triangle
    _prototypeSpaceship.graphics.moveTo(-30,35 );
    _prototypeSpaceship.graphics.lineTo(0, 60);
    _prototypeSpaceship.graphics.lineTo(30, 35);
    _prototypeSpaceship.graphics.lineTo(-30,35);
    _prototypeSpaceship.graphics.drawEllipse(-35, 24.5, 70, 21.5);
//    _prototypeSpaceship.graphics.drawRect(-35,25,70,10);
  }

  private var _displacementMapFilter:DisplacementMapFilter;

  private function onFrame(event:Event):void {
    _trailBitmapData.lock();
    _trailBitmapData.copyPixels(_trailBitmapData, _trailBitmapData.rect, new Point(0,0));
    _trailBitmapData.applyFilter(_trailBitmapData, _trailBitmapData.rect, new Point(0,0),
       _blurFilter);
    _trailBitmapData.applyFilter(_trailBitmapData, _trailBitmapData.rect, new Point(0,0),
      _displacementMapFilter);
    _trailBitmapData.scroll(_wind.windX * 2.5, _wind.windY * 2.5);
    _trailBitmapData.unlock();
  }

  private function setupDisplacementMap():void {
    _noiseBitmapData = _trailBitmapData.clone();
    _noiseBitmapSeed = int(new Date());
    _offsets = [new Point(0,0), new Point(0,0)];
    renderNoise();
    _displacementMapFilter =  new DisplacementMapFilter(_noiseBitmapData, new Point(0,0), BitmapDataChannel.RED,
      BitmapDataChannel.RED, 5.0, 5.0, DisplacementMapFilterMode.CLAMP, 0, 0);
    TweenLite.delayedCall(4, setupDisplacementMap);

  }

  private function setupBitmapData():void {
    _blurFilter = new BlurFilter(5.0, 5.0, 1 );
    _trailBitmapData = new BitmapData(HangoutInSpace.HANGOUT_WIDTH, HangoutInSpace.HANGOUT_HEIGHT, true, 0x00000000);
    this.addChild(new Bitmap(_trailBitmapData));
    setupDisplacementMap();
  }

  private function renderNoise():void {
    _noiseBitmapData.perlinNoise(64, 64, 2, _noiseBitmapSeed, false, false,
      BitmapDataChannel.RED, true, _offsets);

  }



  public function addDots(trailData:Array):void {
    for each (var nextData:TrailData in trailData) {
      var transformMatrix:Matrix = new Matrix();

      // Size is between 0 and 100
      var scaleFactor:Number = .2 + (nextData.size / 100.0) * 0.9;
      transformMatrix.scale(scaleFactor, scaleFactor);

      transformMatrix.rotate(nextData.newRotation * Math.PI / 180.0);
      transformMatrix.translate(nextData.newPosition.x, nextData.newPosition.y);


      var redVal:uint = (nextData.color & 0x00FF0000) >>  16;
      var greenVal:uint = (nextData.color & 0x0000FF00) >> 8;
      var blueVal:uint = (nextData.color & 0x000000FF) ;

      var colorTrans:ColorTransform = new ColorTransform(1,1,1,1,redVal,greenVal,blueVal);
      _trailBitmapData.draw(_prototypeSpaceship, transformMatrix, colorTrans);

    }
  }

}
}
